
package com.rt.schedulenew.utils.base;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.http.HttpMethod;
import org.springframework.util.MultiValueMap;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import java.util.Map;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import org.slf4j.Logger;
public class RestTemplateUtil
{
    private static final Logger log;
    static RestTemplate restTemplate;
    
    public static <T> ResponseEntity<T> get(final String url, final Class<T> responseType) {
        return (ResponseEntity<T>)RestTemplateUtil.restTemplate.getForEntity(url, (Class)responseType, new Object[0]);
    }
    
    public static <T> ResponseEntity<T> get(final String url, final Class<T> responseType, final Object... uriVariables) {
        return (ResponseEntity<T>)RestTemplateUtil.restTemplate.getForEntity(url, (Class)responseType, uriVariables);
    }
    
    public static <T> ResponseEntity<T> get(final String url, final Class<T> responseType, final Map<String, ?> uriVariables) {
        return (ResponseEntity<T>)RestTemplateUtil.restTemplate.getForEntity(url, (Class)responseType, (Map)uriVariables);
    }
    
    public static <T> ResponseEntity<T> get(final String url, final Map<String, String> headers, final Class<T> responseType, final Object... uriVariables) {
        final HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setAll((Map)headers);
        return get(url, httpHeaders, responseType, uriVariables);
    }
    
    public static <T> ResponseEntity<T> get(final String url, final HttpHeaders headers, final Class<T> responseType, final Object... uriVariables) {
        final HttpEntity<?> requestEntity = (HttpEntity<?>)new HttpEntity((MultiValueMap)headers);
        return exchange(url, HttpMethod.GET, requestEntity, responseType, uriVariables);
    }
    
    public static <T> ResponseEntity<T> get(final String url, final Map<String, String> headers, final Class<T> responseType, final Map<String, ?> uriVariables) {
        final HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setAll((Map)headers);
        return get(url, httpHeaders, responseType, uriVariables);
    }
    
    public static <T> ResponseEntity<T> get(final String url, final HttpHeaders headers, final Class<T> responseType, final Map<String, ?> uriVariables) {
        final HttpEntity<?> requestEntity = (HttpEntity<?>)new HttpEntity((MultiValueMap)headers);
        return exchange(url, HttpMethod.GET, requestEntity, responseType, uriVariables);
    }
    
    public static String postObject(final String url, final String bodyContent) {
        final HttpHeaders headers = new HttpHeaders();
        final MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        final HttpEntity<String> entity = (HttpEntity<String>)new HttpEntity((Object)bodyContent, (MultiValueMap)headers);
        final String result = (String)RestTemplateUtil.restTemplate.postForObject(url, (Object)entity, (Class)String.class, new Object[0]);
        return result;
    }
    
    public static <T> ResponseEntity<T> post(final String url, final Class<T> responseType) {
        return (ResponseEntity<T>)RestTemplateUtil.restTemplate.postForEntity(url, (Object)HttpEntity.EMPTY, (Class)responseType, new Object[0]);
    }
    
    public static <T> ResponseEntity<T> post(final String url, final Object requestBody, final Class<T> responseType) {
        return (ResponseEntity<T>)RestTemplateUtil.restTemplate.postForEntity(url, requestBody, (Class)responseType, new Object[0]);
    }
    
    public static <T> ResponseEntity<T> post(final String url, final Object requestBody, final Class<T> responseType, final Object... uriVariables) {
        return (ResponseEntity<T>)RestTemplateUtil.restTemplate.postForEntity(url, requestBody, (Class)responseType, uriVariables);
    }
    
    public static <T> ResponseEntity<T> post(final String url, final Object requestBody, final Class<T> responseType, final Map<String, ?> uriVariables) {
        return (ResponseEntity<T>)RestTemplateUtil.restTemplate.postForEntity(url, requestBody, (Class)responseType, (Map)uriVariables);
    }
    
    public static <T> ResponseEntity<T> post(final String url, final Map<String, String> headers, final Object requestBody, final Class<T> responseType, final Object... uriVariables) {
        final HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setAll((Map)headers);
        return post(url, httpHeaders, requestBody, responseType, uriVariables);
    }
    
    public static <T> ResponseEntity<T> post(final String url, final HttpHeaders headers, final Object requestBody, final Class<T> responseType, final Object... uriVariables) {
        final HttpEntity<Object> requestEntity = (HttpEntity<Object>)new HttpEntity(requestBody, (MultiValueMap)headers);
        return post(url, requestEntity, responseType, uriVariables);
    }
    
    public static <T> ResponseEntity<T> post(final String url, final Map<String, String> headers, final Object requestBody, final Class<T> responseType, final Map<String, ?> uriVariables) {
        final HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setAll((Map)headers);
        return post(url, httpHeaders, requestBody, responseType, uriVariables);
    }
    
    public static <T> ResponseEntity<T> post(final String url, final HttpHeaders headers, final Object requestBody, final Class<T> responseType, final Map<String, ?> uriVariables) {
        final HttpEntity<Object> requestEntity = (HttpEntity<Object>)new HttpEntity(requestBody, (MultiValueMap)headers);
        return post(url, requestEntity, responseType, uriVariables);
    }
    
    public static <T> ResponseEntity<T> post(final String url, final HttpEntity<?> requestEntity, final Class<T> responseType, final Object... uriVariables) {
        return (ResponseEntity<T>)RestTemplateUtil.restTemplate.exchange(url, HttpMethod.POST, (HttpEntity)requestEntity, (Class)responseType, uriVariables);
    }
    
    public static <T> ResponseEntity<T> post(final String url, final HttpEntity<?> requestEntity, final Class<T> responseType, final Map<String, ?> uriVariables) {
        return (ResponseEntity<T>)RestTemplateUtil.restTemplate.exchange(url, HttpMethod.POST, (HttpEntity)requestEntity, (Class)responseType, (Map)uriVariables);
    }
    
    public static <T> ResponseEntity<T> exchange(final String url, final HttpMethod method, final HttpEntity<?> requestEntity, final Class<T> responseType, final Object... uriVariables) {
        return (ResponseEntity<T>)RestTemplateUtil.restTemplate.exchange(url, method, (HttpEntity)requestEntity, (Class)responseType, uriVariables);
    }
    
    public static <T> ResponseEntity<T> exchange(final String url, final HttpMethod method, final HttpEntity<?> requestEntity, final Class<T> responseType, final Map<String, ?> uriVariables) {
        return (ResponseEntity<T>)RestTemplateUtil.restTemplate.exchange(url, method, (HttpEntity)requestEntity, (Class)responseType, (Map)uriVariables);
    }
    
    public static RestTemplate getRestTemplate() {
        return RestTemplateUtil.restTemplate;
    }
    
    static {
        log = LoggerFactory.getLogger((Class)RestTemplateUtil.class);
        RestTemplateUtil.restTemplate = null;
        final SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        RestTemplateUtil.restTemplate = new RestTemplate((ClientHttpRequestFactory)requestFactory);
    }
}
